package com.request; /**
 * @author wzm
 * @create 2023-03-06 10:49
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Map;

@WebServlet("/rs04")
public class RequestServlet04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //不用重写写代码 调用即可
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //Post解决请求参数的中文乱码  在获取参数之前设置超参数的编码  (tomcat7get已经解决了)
        request.setCharacterEncoding("UTF-8");

        //通用的获取请求数据的代码

        //1.通过请求参数的名字获取单个值
        String username = request.getParameter("username");
        System.out.println("username = " + username);
        String password = request.getParameter("password");
        System.out.println("password = " + password);

        System.out.println("--------------------------------------");


        //2.request.getParameterValues() 根据名称获取参数的数组 适用于多个值
        String[] hobbies = request.getParameterValues("hobby");
       for (String s : hobbies){
           System.out.println("s = " + s);
       }
        System.out.println("-------------------------------");


       //3.request.getParameterMap();将所有参数获取作为一个Map集合
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println("parameterMap = " + parameterMap);
        for (String key:parameterMap.keySet()){
            //获取map集合的key username:lisi  hobby:[111,222]
            System.out.println("key : "+key+"=");

            //有了键以后根据key获取值
            String[] strings = parameterMap.get(key);
            for (String str:strings){
                System.out.println(str + "  ");
            }

        }


    }
}
